home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Utils / GParted Live CD / Bin / gparted-livecd-0.2.2.iso / usr_sqfs / bin / guile-config < prev    next >
Encoding:
Text File  |  2005-07-27  |  10.3 KB  |  300 lines

  1. #!/usr/bin/guile \
  2. -e main -s
  3. !#
  4. ;;;; guile-config --- utility for linking programs with Guile
  5. ;;;; Jim Blandy <jim@red-bean.com> --- September 1997
  6. ;;;; 
  7. ;;;;     Copyright (C) 1998, 2001 Free Software Foundation, Inc.
  8. ;;;; 
  9. ;;;; This program is free software; you can redistribute it and/or modify
  10. ;;;; it under the terms of the GNU General Public License as published by
  11. ;;;; the Free Software Foundation; either version 2, or (at your option)
  12. ;;;; any later version.
  13. ;;;; 
  14. ;;;; This program is distributed in the hope that it will be useful,
  15. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;;;; GNU General Public License for more details.
  18. ;;;; 
  19. ;;;; You should have received a copy of the GNU General Public License
  20. ;;;; along with this software; see the file COPYING.  If not, write to
  21. ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  22. ;;;; Boston, MA 02111-1307 USA
  23. ;;;; 
  24. ;;;; As a special exception, the Free Software Foundation gives permission
  25. ;;;; for additional uses of the text contained in its release of GUILE.
  26. ;;;; 
  27. ;;;; The exception is that, if you link the GUILE library with other files
  28. ;;;; to produce an executable, this does not by itself cause the
  29. ;;;; resulting executable to be covered by the GNU General Public License.
  30. ;;;; Your use of that executable is in no way restricted on account of
  31. ;;;; linking the GUILE library code into it.
  32. ;;;; 
  33. ;;;; This exception does not however invalidate any other reasons why
  34. ;;;; the executable file might be covered by the GNU General Public License.
  35. ;;;; 
  36. ;;;; This exception applies only to the code released by the
  37. ;;;; Free Software Foundation under the name GUILE.  If you copy
  38. ;;;; code from other Free Software Foundation releases into a copy of
  39. ;;;; GUILE, as the General Public License permits, the exception does
  40. ;;;; not apply to the code that you add in this way.  To avoid misleading
  41. ;;;; anyone as to the status of such modified files, you must delete
  42. ;;;; this exception notice from them.
  43. ;;;; 
  44. ;;;; If you write modifications of your own for GUILE, it is your choice
  45. ;;;; whether to permit this exception to apply to your modifications.
  46. ;;;; If you do not wish that, delete this exception notice.
  47.  
  48. ;;; TODO:
  49. ;;; * Add some plausible structure for returning the right exit status,
  50. ;;;   just something that encourages people to do the correct thing.
  51. ;;; * Implement the static library support.  This requires that
  52. ;;;   some portion of the module system be done.
  53.  
  54. (use-modules (ice-9 string-fun))
  55.  
  56.  
  57. ;;;; main function, command-line processing
  58.  
  59. ;;; The script's entry point.
  60. (define (main args)
  61.   (set-program-name! (car args))
  62.   (let ((args (cdr args)))
  63.     (cond
  64.      ((null? args) (show-help '())
  65.            (quit 1))
  66.      ((assoc (car args) command-table)
  67.       => (lambda (row)
  68.        (set! subcommand-name (car args))
  69.        ((cadr row) (cdr args))))
  70.      (else (show-help '())
  71.        (quit 1)))))
  72.  
  73. (define program-name #f)
  74. (define subcommand-name #f)
  75. (define program-version "1.6.7")
  76.  
  77. ;;; Given an executable path PATH, set program-name to something
  78. ;;; appropriate f or use in error messages (i.e., with leading
  79. ;;; directory names stripped).
  80. (define (set-program-name! path)
  81.   (set! program-name (basename path)))
  82.  
  83. (define (show-help args)
  84.   (cond
  85.    ((null? args) (show-help-overview))
  86.    ((assoc (car args) command-table)
  87.     => (lambda (row) ((caddr row))))
  88.    (else
  89.     (show-help-overview))))
  90.  
  91. (define (show-help-overview)
  92.   (display-line-error "Usage: ")
  93.   (for-each (lambda (row) ((cadddr row)))
  94.         command-table))
  95.  
  96. (define (usage-help)
  97.   (let ((dle display-line-error)
  98.     (p program-name))
  99.     (dle "  " p " --help      - show usage info (this message)")
  100.     (dle "  " p " --help SUBCOMMAND - show help for SUBCOMMAND")))
  101.  
  102. (define (show-version args)
  103.   (display-line-error program-name " - Guile version " program-version))
  104.  
  105. (define (help-version)
  106.   (let ((dle display-line-error))
  107.     (dle "Usage: " program-name " --version")
  108.     (dle "Show the version of this script.  This is also the version of")
  109.     (dle "Guile this script was installed with.")))
  110.  
  111. (define (usage-version)
  112.   (display-line-error
  113.    "  " program-name " --version   - show installed script and Guile version"))
  114.  
  115.  
  116. ;;;; the "link" subcommand
  117.  
  118. ;;; Write a set of linker flags to standard output to include the
  119. ;;; libraries that libguile needs to link against.
  120. ;;;
  121. ;;; In the long run, we want to derive these flags from Guile module
  122. ;;; declarations files that are installed along the load path.  For
  123. ;;; now, we're just going to reach into Guile's configuration info and
  124. ;;; hack it out.
  125. (define (build-link args)
  126.  
  127.   ;; If PATH has the form FOO/libBAR.a, return the substring
  128.   ;; BAR, otherwise return #f.
  129.   (define (match-lib path)
  130.     (let* ((base (basename path))
  131.        (len (string-length base)))
  132.       (if (and (> len 5)
  133.            (string=? (substring base 0 3) "lib")
  134.            (string=? (substring base (- len 2)) ".a"))
  135.       (substring base 3 (- len 2))
  136.       #f)))
  137.  
  138.   (if (> (length args) 0)
  139.       (error
  140.        (string-append program-name
  141.               " link: arguments to subcommand not yet implemented")))
  142.  
  143.   (let ((libdir (get-build-info 'libdir))
  144.         (other-flags
  145.          (let loop ((libs
  146.                      ;; Get the string of linker flags we used to build
  147.                      ;; Guile, and break it up into a list.
  148.                      (separate-fields-discarding-char #\space
  149.                                                       (get-build-info 'LIBS)
  150.                                                       list)))
  151.             
  152.            (cond
  153.             ((null? libs) '())
  154.             
  155.             ;; Turn any "FOO/libBAR.a" elements into "-lBAR".
  156.             ((match-lib (car libs))
  157.              => (lambda (bar)
  158.                   (cons (string-append "-l" bar)
  159.                         (loop (cdr libs)))))
  160.             
  161.             ;; Remove any empty strings that may have seeped in there.
  162.             ((string=? (car libs) "") (loop (cdr libs)))
  163.             
  164.             (else (cons (car libs) (loop (cdr libs))))))))
  165.     
  166.     ;; Include libguile itself in the list, along with the directory
  167.     ;; it was installed in, but do *not* add /usr/lib since that may
  168.     ;; prevent other programs from specifying non-/usr/lib versions
  169.     ;; via their foo-config scripts.  If *any* app puts -L/usr/lib in
  170.     ;; the output of its foo-config script then it may prevent the use
  171.     ;; a non-/usr/lib install of anything that also has a /usr/lib
  172.     ;; install. For now we hard-code /usr/lib, but later maybe we can
  173.     ;; do something more dynamic (i.e. what do we need.
  174.     
  175.     ;; Display the flags, separated by spaces.
  176.     (if (or (string=? libdir "/usr/lib")
  177.             (string=? libdir "/usr/lib/"))
  178.         (display-separated (cons "-lguile -lguile-ltdl" other-flags))
  179.         (display-separated (cons
  180.                             (string-append "-L" (get-build-info 'libdir))
  181.                             (cons "-lguile -lguile-ltdl" other-flags))))
  182.     (newline)))
  183.  
  184. (define (help-link)
  185.   (let ((dle display-line-error))
  186.     (dle "Usage: " program-name " link")
  187.     (dle "Print linker flags for building the `guile' executable.")
  188.     (dle "Print the linker command-line flags necessary to link against")
  189.     (dle "the Guile library, and any other libraries it requires.")))
  190.  
  191. (define (usage-link)
  192.   (display-line-error
  193.    "  " program-name " link        - print libraries to link with"))
  194.  
  195.  
  196.  
  197. ;;;; The "compile" subcommand
  198.  
  199. (define (build-compile args)
  200.   (if (> (length args) 0)
  201.       (error
  202.        (string-append program-name
  203.               " compile: no arguments expected")))
  204.  
  205.   ;; See gcc manual wrt fixincludes.  Search for "Use of
  206.   ;; `-I/usr/include' may cause trouble."  For now we hard-code this.
  207.   ;; Later maybe we can do something more dynamic.
  208.   (if (not (string=? (get-build-info 'includedir) "/usr/include"))
  209.       (display-line "-I" (get-build-info 'includedir))))
  210.  
  211. (define (help-compile)
  212.   (let ((dle display-line-error))
  213.     (dle "Usage: " program-name " compile")
  214.     (dle "Print C compiler flags for compiling code that uses Guile.")
  215.     (dle "This includes any `-I' flags needed to find Guile's header files.")))
  216.  
  217. (define (usage-compile)
  218.   (display-line-error
  219.    "  " program-name " compile     - print C compiler flags to compile with"))
  220.  
  221.  
  222. ;;;; The "info" subcommand
  223.  
  224. (define (build-info args)
  225.   (cond
  226.    ((null? args) (show-all-vars))
  227.    ((null? (cdr args)) (show-var (car args)))
  228.    (else (display-line-error "Usage: " program-name " info [VAR]")
  229.      (quit 2))))
  230.  
  231. (define (show-all-vars)
  232.   (for-each (lambda (binding)
  233.           (display-line (car binding) " = " (cdr binding)))
  234.         %guile-build-info))
  235.  
  236. (define (show-var var)
  237.   (display (get-build-info (string->symbol var)))
  238.   (newline))
  239.  
  240. (define (help-info)
  241.   (let ((d display-line-error))
  242.     (d "Usage: " program-name " info [VAR]")
  243.     (d "Display the value of the Makefile variable VAR used when Guile")
  244.     (d "was built.  If VAR is omitted, display all Makefile variables.")
  245.     (d "Use this command to find out where Guile was installed,")
  246.     (d "where it will look for Scheme code at run-time, and so on.")))
  247.  
  248. (define (usage-info)
  249.   (display-line-error
  250.    "  " program-name " info [VAR]  - print Guile build directories"))
  251.  
  252.  
  253. ;;;; trivial utilities
  254.  
  255. (define (get-build-info name)
  256.   (let ((val (assq name %guile-build-info)))
  257.     (if (not (pair? val))
  258.     (begin
  259.       (display-line-error
  260.        program-name " " subcommand-name ": no such build-info: " name)
  261.       (quit 2)))
  262.     (cdr val)))
  263.  
  264. (define (display-line . args)
  265.   (apply display-line-port (current-output-port) args))
  266.  
  267. (define (display-line-error . args)
  268.   (apply display-line-port (current-error-port) args))
  269.  
  270. (define (display-line-port port . args)
  271.   (for-each (lambda (arg) (display arg port))
  272.         args)
  273.   (newline port))
  274.  
  275. (define (display-separated args)
  276.   (if (not (null? args))
  277.       (begin
  278.         (display (car args))
  279.         (for-each
  280.          (lambda (arg) (display " ") (display arg))
  281.          (cdr args)))))
  282.  
  283.  
  284. ;;;; the command table
  285.  
  286. ;;; We define this down here, so Guile builds the list after all the
  287. ;;; functions have been defined.
  288. (define command-table
  289.   (list
  290.    (list "--version" show-version help-version usage-version)
  291.    (list "--help" show-help show-help-overview usage-help)
  292.    (list "link" build-link help-link usage-link)
  293.    (list "compile" build-compile help-compile usage-compile)
  294.    (list "info" build-info help-info usage-info)))
  295.  
  296.  
  297. ;;; Local Variables:
  298. ;;; mode: scheme
  299. ;;; End:
  300.